package coba.mailing.mail;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import coba.mailing.body.Body4;
import coba.mailing.function.NextXday;
import coba.mailing.jdbc.ConnectionFactory;
import coba.mailing.model.Guia;
import coba.mailing.model.GuiaSolicitud;
import coba.mailing.procedure.AddAllCC;
import coba.mailing.procedure.AddAllTo;
import coba.mailing.properties.MainProperties;
import coba.mailing.query.FindAllGuia;
import coba.mailing.query.FindGuiaSolicitudWhereFechaItinerarioBetween;

public class Mail4 {

	public static void send() throws ClassNotFoundException, IOException, SQLException, EmailException {
		Connection con = ConnectionFactory.getConnection();

		Calendar today0Hour = GregorianCalendar.getInstance();
		
		today0Hour.set(Calendar.HOUR_OF_DAY, 0);
		today0Hour.set(Calendar.MINUTE, 0);
		today0Hour.set(Calendar.SECOND, 0);
		today0Hour.set(Calendar.MILLISECOND, 0);

		Date mondayNextWeek = NextXday.nextXday(Calendar.MONDAY, today0Hour.getTime());
		Date saturdayNextWeek = NextXday.nextXday(Calendar.SATURDAY, mondayNextWeek);

		List<GuiaSolicitud> listGuiaSolicitud = FindGuiaSolicitudWhereFechaItinerarioBetween.
				find(con, mondayNextWeek, saturdayNextWeek);

		List<Guia> listGuia = FindAllGuia.find(con);

		if (!listGuiaSolicitud.isEmpty())
			send(mondayNextWeek, saturdayNextWeek, listGuia, listGuiaSolicitud);
		
		con.close();
	}

	public static void send(Date monday, Date saturday, List<Guia> listGuia, List<GuiaSolicitud> listGuiaSolicitud) throws EmailException, IOException {
		MainProperties mainProperties = MainProperties.getInstance();

		HtmlEmail email = new HtmlEmail();

		email.setCharset("utf-8");
		email.setHostName(mainProperties.getProperty("smtpServer"));
		email.setSmtpPort(Integer.valueOf(mainProperties.getProperty("smtpPort")));
		
		email.setFrom(mainProperties.getProperty("fromMail"), mainProperties.getProperty("fromName"));

		AddAllTo.addAllTo(email, mainProperties.getProperty("mail4Tos"));
		AddAllCC.addAllCC(email, mainProperties.getProperty("mail4Ccs"));
		
		email.setSubject(subject(monday, saturday));

		String cid = email.embed(ClassLoader.getSystemClassLoader().getResource("coba-mail-footer.jpg"), "Conociendo footer");

		email.setHtmlMsg(Body4.get(cid, listGuia, listGuiaSolicitud));

		email.send();
	}

	private static String subject(Date monday, Date saturday) {

		String[] arrMes = { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio",
				"Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" };

		StringBuilder subject = new StringBuilder("ConociendoBA - Grupos Semana del ");
		
		Calendar calMonday = GregorianCalendar.getInstance();
		calMonday.setTime(monday);

		subject.append(calMonday.get(Calendar.DAY_OF_MONTH));

		subject.append(" al ");

		Calendar calSaturday = GregorianCalendar.getInstance();
		calSaturday.setTime(saturday);

		subject.append(calSaturday.get(Calendar.DAY_OF_MONTH));

		subject.append(" de ");

		subject.append(arrMes[calSaturday.get(Calendar.MONTH)]);

		return subject.toString();
	}

}
